import { createRouter, createWebHistory } from 'vue-router'
const Home  = ()=>import('../views/home/Home');
const Detail  = ()=>import('../views/detail/Detail');
const Profile  = ()=>import('../views/profile/Login');
const ShopCart  = ()=>import('../views/shopcart/ShopCart');
const BookSort  = ()=>import('../views/booksort/BookSort');
const Register = () => import('../views/profile/Register');
const Createorder = () => import('../views/shopcart/Createorder');
const Address =()=> import ('../views/profile/Address/Address');
const addAddress =() => import('../views/profile/Address/editAddress');
const order =()=> import('../views/profile/Order/order');
const orderDetail =()=> import('../views/profile/Order/orderDetail');
const EditAddress =()=> import ('../views/profile/Address/editAddress');
const Login = () => import('../views/profile/Login');
import store from '../store';
const User = () => import('../views/profile/User');
import {Notify} from 'vant'
const routes = [
  {
    path:'/orderDetail',
    component:orderDetail,
    meta:{
      title:'订单详情'
    }
  },
  {
    path:'/order',
    component:order,
    meta:{
      title:'订单查看'
    }
  },
  {
    path: '',
    redirect:'home'
  },
  {
    path: '/Home',
    name: 'Home',
    component: Home,
    meta:{
      title:'图书兄弟-首页',
    }
  },
  {
    path: '/booksort',
    name: 'BookSort',
    component: BookSort,
    meta:{
      title:'图书兄弟--商品分类页',

    }

  },
  {
    path: '/detail',
    name: 'Detail',
    component: Detail,
    meta:{
      title:'图书兄弟--商品详情页',

    }
  },
  {
    path: '/user',
    name: 'User',
    component: User,
    meta:{
      title:'图书兄弟--用户中心页',
      isAuthReqyired:true
    }
  },
  {
    path: '/shopcart',
    name: 'ShopCart',
    component:  ShopCart,
    meta:{
      title:'图书兄弟--购物车',

    }
  },
  {
    path: '/register',
    name: 'Register',
    component:  Register,
    meta:{
      title:'图书兄弟--新用户注册'
    }
  },
  {
    path: '/Login',
    name: 'Login',
    component:  Login,
    meta:{
      title:'图书兄弟--用户登录',
    }
  },
  {
    path: '/Createorder',
    name: 'Createorder',
    component:  Createorder,
    meta:{
      title:'账单结算',
    }
  },
  {
    path: '/Address',
    name: 'Address',
    component:  Address,
    meta:{
      title:'地址管理',
    }
  },
  {
    path: '/EditAddress',
    name: 'EditAddress',
    component: EditAddress,
    meta:{
      title:'修改地址',
    }
  },
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})



//导航守位
router.beforeEach((to,from,next)=>{
  if (to.meta.isAuthReqyired && store.state.user.islogin===false){
    Notify('您还没有登录，请先登录！');
    return next('/login')
  }else{
    next();
  }
  //如果没有登录，在这里跳到login

  document.title=to.meta.title;
})
export default router
